package cn.sunlight.mm.service.impl;

import cn.sunlight.framework.annotation.HmComponent;
import cn.sunlight.mm.base.BaseService;
import cn.sunlight.mm.dao.CourseDao;
import cn.sunlight.mm.dao.TagDao;
import cn.sunlight.mm.database.MmDaoException;
import cn.sunlight.mm.entity.PageResult;
import cn.sunlight.mm.entity.QueryPageBean;
import cn.sunlight.mm.pojo.Course;
import cn.sunlight.mm.service.CourseService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

/**
 * @author sunlight
 * @description:
 * @date 2021/5/19
 * @Version v1.0
 */
@Slf4j
@HmComponent("courseService")
public class CourseServiceImpl extends BaseService implements CourseService {

    @Override
    public void addCourse(Course course) {
        log.debug("Course:{}",course);
        SqlSession session = getSession();
        try {
            CourseDao courseDao = getDao(session, CourseDao.class);
            Integer result=courseDao.insertCourse(course);
            if (result==0){
                throw new MmDaoException("添加失败");
            }
            //提交事务
            commitAndCloseSession(session);
        }catch (MmDaoException e){
            //回滚事务
            rollbackAndCloseSession(session);
            e.printStackTrace();
            log.error("",e);
        }
    }

    @Override
    public PageResult findListByPage(QueryPageBean queryPageBean) {
        try {
            log.debug("queryPageBean:{}",queryPageBean);
            SqlSession session = getSession();
            CourseDao courseDao = getDao(session, CourseDao.class);
            //获取结果集
            List<Course> courseList=courseDao.selectListByPage(queryPageBean);
            //获取记录数
            Long total=courseDao.selectCourse(queryPageBean);

            return new PageResult(total,courseList);
        }catch (MmDaoException e){
            e.printStackTrace();
            log.error(e.getMessage());
            throw  new MmDaoException(e.getMessage());
        }

    }

    @Override
    public void updateCourse(Course course) {
        SqlSession session = getSession();
        try {
            CourseDao courseDao = getDao(session, CourseDao.class);
            // 调用方法完成更新
            courseDao.updateCourse(course);
            //提交保存
            commitAndCloseSession(session);
        }catch (RuntimeException e){
            e.printStackTrace();
            rollbackAndCloseSession(session);
            log.error(e.getMessage());
            throw  new MmDaoException(e.getMessage());
        }
    }

    @Override
    public List<Course> findListAll() {
        try {
            SqlSession session = getSession();
            CourseDao courseDao = getDao(session, CourseDao.class);
            List<Course> courseList=courseDao.selectListAll();
            session.close();
            return courseList;
        }catch (MmDaoException e){
            e.printStackTrace();
            log.error("",e.getMessage());
            throw new MmDaoException(e.getMessage());
        }
    }

}
